<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{asset('lib/layui-v2.6.3/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/public.css')}}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label required">商品分类</label>
            <div class="layui-input-inline">
                <button class="layui-btn" id="good-cate-list">
                    选择分类
                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                </button>
            </div>
            <div class="layui-input-inline">
                <input type="text" id="cate-title" disabled class="layui-input">
                <input type="hidden" id="cate_id" name="cate_id" disabled class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">商品名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="商品名不能为空" placeholder="请输入商品名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">市场价</label>
        <div class="layui-input-block">
            <input type="text" name="price" lay-verify="required|number" class="layui-input" onblur="clearNoNum(this)">
            <tip>小数点支持到两位。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">一级代理价</label>
        <div class="layui-input-block">
            <input type="text" name="price_agent_one" lay-verify="required" class="layui-input" onblur="clearNoNum(this)">
            <tip>小数点支持到两位。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">二级代理价</label>
        <div class="layui-input-block">
            <input type="text" name="price_agent_two" lay-verify="required" class="layui-input" onblur="clearNoNum(this)">
            <tip>小数点支持到两位。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">商品图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="good-img">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <tip>图片文件大小不超过2M。允许的图片jpg|png|gif|bmp|jpeg</tip>
        </div>
        <div class="layui-input-block" id="show-img"></div>
    </div>
    <input type="hidden" name="good_img" id="good_img">
    <input type="hidden" name="good_img_small" id="good_img_small">
    <div class="layui-form-item">
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
        <div class="wang-editor" id="wang-editor"></div>
        </div>
    </div>
    @csrf
    <textarea id="editor_content" name="detail" style="display: none;"></textarea>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" id="good-add-button">确认保存</button>
        </div>
    </div>
</div>

<script src="{{asset('lib/layui-v2.6.3/layui.js')}}" charset="utf-8"></script>
<script src="{{asset('lib/jquery-3.4.1/jquery-3.4.1.min.js')}}" charset="utf-8"></script>
<script src="{{asset('js/lay-module/wangEditor/wangEditor.min.js')}}"></script>
<script src="{{asset('js/app.js')}}" charset="utf-8"></script>
<script>
    //富文本编辑器
    const E = window.wangEditor;
    const editor = new E("#wang-editor");
    editor.config.height = 400;
    editor.config.uploadFileName = 'good-img';
    editor.config.uploadImgServer = '/api/goods/upload';
    editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
    editor.config.uploadImgMaxLength = 1;
    editor.config.uploadImgParams = {'editor': true};
    editor.config.excludeMenus = [
        'emoticon',
        'video'
    ];
    let editor_content = $("#editor_content");
    editor.config.onchange = (html) => {
        editor_content.val(html);
    }
    editor.create();

        layui.use(['form', 'upload', 'dropdown'], function () {
        let form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            dropdown = layui.dropdown,
            $ = layui.$;
        //渲染分类
            dropdown.render({
                elem: '#good-cate-list',
                data: {!! $data !!},
                click: function (obj) {
                    $("#cate-title").val(obj.title);
                    $("#cate_id").val(obj.id);
                    //console.log(obj);
                }
            });
        //监听上传
            upload.render({
                elem: '#good-img',
                url: '/api/goods/upload',
                accept: 'images',
                acceptMime: 'image/*',
                exts: 'jpg|png|gif|bmp|jpeg',
                size: 2048,
                field: 'good-img',
                done: function (res) {
                    if (res.code === 0) {
                        $("#show-img").html('<img src=' + res.data.sSrc + '>');
                        $("#good_img").val(res.data.src);
                        $("#good_img_small").val(res.data.sSrc);
                    } else {
                        layer.msg('上传失败');
                    }
                },
                error: function (status) {
                    layer.msg('上传失败');
                }
            });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            disabledButton('good-add-button');
            $.ajax({
                type: 'POST',
                url: '/api/goods/store',
                dataType: 'json',
                data: data.field,
                success:function (res) {
                    if (res.code == 200) {
                        layer.closeAll();
                        parent.location.reload();
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    } else {
                        layer.open({
                            title: '出问题了'
                            ,content: '网络阻塞，请稍后再试'
                        });
                    }
                    undisabledButton('good-add-button');
                },
                //处理422
                error:function (err) {
                    if (err.status == 422) {
                        let json = JSON.parse(err.responseText);
                        json = json.errors;
                        let errorInfo = [];
                        for (let item in json) {
                            for (let i = 0; i < json[item].length; i++) {
                                errorInfo.push(json[item][i]);
                            }
                        }
                        let errStr = errorInfo.join('<br>');
                        layer.open({
                            title: '填写错误'
                            ,content: errStr
                        });
                    }
                    undisabledButton('good-add-button');
                    return false;
                },
                complete:function () {

                },
            });
            return false;
        });

    });

</script>
</body>
</html>
